﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using OpenSoftStore.Orm;
using OpenSoftStore.Orm.Model;
using System.Linq;
using OpenSoftStore.Orm.View;
using System.Data;

namespace OpenSoftStore.BusinessLogic.Service
{
    public class ServiceLogic : ABusinessLogic
    {
        public ServiceLogic()
        {
        }

        #region Methods

        #region Service
        /// <summary>
        /// 读取服务列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public IEnumerable<V_Product> GetServiceView(string strWhere = "")
        {
            string sql = "select P.*,E.ExtId,E.BrokerageType,E.BrokerageNum,E.BarCode,E.SalePrice,E.ChargeMark,T.Name as TreeName  from";
            sql += "  (T_Product as P inner join T_ProductExt as E on P.Id=E.ProductId)";
            sql += "  inner join T_Tree as T on T.Id = P.TreeId  where P.IsProject='是'  ";
            sql += strWhere;
            sql += " order by P.AddTime desc ";
            return Spa.SelectWhere<V_Product>(sql);
        }

        public DataTable GetServiceDataTable(string strWhere = "")
        {
            string sql = "select P.*,E.ExtId,E.BrokerageType,E.BrokerageNum,E.BarCode,E.SalePrice,E.ChargeMark,T.Name as TreeName  from";
            sql += "  (T_Product as P inner join T_ProductExt as E on P.Id=E.ProductId)";
            sql += "  inner join T_Tree as T on T.Id = P.TreeId  where P.IsProject='是'  ";
            sql += strWhere;
            sql += " order by P.AddTime desc ";
            var data = Spa.RunSqLcmdDataSet(sql);
            if (data.Tables == null || data.Tables.Count == 0) return new DataTable();
            return data.Tables[0];
        }

        /// <summary>
        /// 添加服务
        /// </summary>
        /// <param name="product"></param>
        /// <param name="productExt"></param>
        /// <returns></returns>
        public int AddService(T_Product service, T_ProductExt productExt, DataTable products)
        {
            Spa.SetCountNo("T_Product", service.ProductNo);
            Spa.AddObject(service);
            Spa.AddObject(productExt);
            List<T_ServiceWithProduct> list = new List<T_ServiceWithProduct>();
            for (int i = 0; i < products.Rows.Count; i++)
            {
                list.Add(new T_ServiceWithProduct
                {
                    Id = Guid.NewGuid().ToString("D"),
                    ProductId = products.Rows[i]["ProductId"].ToString(),
                    ServiceId = service.Id,
                    Amount = Convert.ToDecimal(products.Rows[i]["Amount"]),
                    Cost = Convert.ToDecimal(products.Rows[i]["Cost"]),
                });
            }
            Spa.AddObjects(list);
            return Spa.SaveChanges();
        }
        /// <summary>
        /// 修改服务
        /// </summary>
        /// <param name="product"></param>
        /// <param name="productExt"></param>
        /// <returns></returns>
        public int UpdateService(T_Product service, T_ProductExt productExt, DataTable products)
        {
            Spa.UpdateObject(service);
            Spa.UpdateObject(productExt);
            var oldProducts = Spa.Where<T_ServiceWithProduct>(s => s.ServiceId == service.Id).ToList();
            Spa.DeleteObjects(oldProducts);
            List<T_ServiceWithProduct> list = new List<T_ServiceWithProduct>();
            for (int i = 0; i < products.Rows.Count; i++)
            {
                list.Add(new T_ServiceWithProduct
                {
                    Id = Guid.NewGuid().ToString("D"),
                    ProductId = products.Rows[i]["ProductId"].ToString(),
                    ServiceId = service.Id,
                    Amount = Convert.ToDecimal(products.Rows[i]["Amount"]),
                    Cost = Convert.ToDecimal(products.Rows[i]["Cost"])
                });
            }
            Spa.AddObjects(list);
            return Spa.SaveChanges();
        }
        /// <summary>
        /// 删除服务
        /// </summary>
        /// <param name="product"></param>
        /// <param name="productExt"></param>
        /// <returns></returns>
        public int DeleteService(T_Product product, T_ProductExt productExt, List<T_ServiceWithProduct> list)
        {
            Spa.DeleteObject(productExt);
            Spa.DeleteObject(product);
            Spa.DeleteObjects(list);
            return Spa.SaveChanges();
        }

        public DataTable GetServiceAndProductTable(string whereStr = "")
        {
            string sql = " select P.Id as ProductId,P.Name,S.Amount,S.Cost from T_Product as P inner join T_ServiceWithProduct as S on P.Id=S.ProductId ";
            var data = Spa.RunSqLcmdDataSet(sql + whereStr);
            if (data.Tables == null || data.Tables.Count == 0) return new DataTable();
            return data.Tables[0];
        }

        #endregion

        #endregion
    }
}

